<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../../style.css" type="text/css" media="screen">
<link rel="stylesheet" href="../../../../print.css" type="text/css" media="print">
<meta content="IComparator,org.as3commons.collections.framework.IComparator,compare" name="keywords">
<title>org.as3commons.collections.framework.IComparator</title>
</head>
<body>
<script type="text/javascript" language="javascript" src="../../../../asdoc.js"></script><script type="text/javascript" language="javascript" src="../../../../cookies.js"></script><script type="text/javascript" language="javascript">
<!--
				asdocTitle = 'IComparator - AS3Commons Collections 1.3.2';
				var baseRef = '../../../../';
				window.onload = configPage;
			--></script>
<table style="display:none" id="titleTable" cellspacing="0" cellpadding="0" class="titleTable">
<tr>
<td align="left" class="titleTableTitle">AS3Commons Collections 1.3.2</td><td align="right" class="titleTableTopNav"><a onclick="loadClassListFrame('../../../../all-classes.html')" href="../../../../package-summary.html">All&nbsp;Packages</a>&nbsp;|&nbsp;<a onclick="loadClassListFrame('../../../../all-classes.html')" href="../../../../class-summary.html">All&nbsp;Classes</a>&nbsp;|&nbsp;<a onclick="loadClassListFrame('../../../../index-list.html')" href="../../../../all-index-A.html">Index</a>&nbsp;|&nbsp;<a href="../../../../index.html?org/as3commons/collections/framework/IComparator.html&amp;org/as3commons/collections/framework/class-list.html" id="framesLink1">Frames</a><a onclick="parent.location=document.location" href="" style="display:none" id="noFramesLink1">No&nbsp;Frames</a></td><td rowspan="3" align="right" class="titleTableLogo"><img alt="Adobe Logo" title="Adobe Logo" class="logoImage" src="../../../../images/logo.jpg"></td>
</tr>
<tr class="titleTableRow2">
<td align="left" id="subTitle" class="titleTableSubTitle">Interface&nbsp;IComparator</td><td align="right" id="subNav" class="titleTableSubNav"><a href="#methodSummary">Methods</a></td>
</tr>
<tr class="titleTableRow3">
<td colspan="2">&nbsp;</td>
</tr>
</table>
<script type="text/javascript" language="javascript">
<!--
if (!isEclipse() || window.name != ECLIPSE_FRAME_NAME) {titleBar_setSubTitle("Interface IComparator"); titleBar_setSubNav(false,false,false,false,false,false,true,false,false,false,false,false,false,false);}
--></script>
<div class="MainContent">
<table cellspacing="0" cellpadding="0" class="classHeaderTable">
<tr>
<td class="classHeaderTableLabel">Package</td><td><a onclick="javascript:loadClassListFrame('class-list.html')" href="package-detail.html">org.as3commons.collections.framework</a></td>
</tr>
<tr>
<td class="classHeaderTableLabel">Interface</td><td class="classSignature">public interface IComparator</td>
</tr>
<tr>
<td class="classHeaderTableLabel">Implementors</td><td><a href="../utils/NullComparator.html">NullComparator</a>, <a href="../utils/NumericComparator.html">NumericComparator</a>, <a href="../utils/StringComparator.html">StringComparator</a></td>
</tr>
</table>
<p></p>
  Comparator definition.
  
  <p><strong>Description</strong></p>
  
  <p>A comparator defines the order of items in sorted collections or in sort operations.</p>
  
  <p><strong>Notes</strong></p>
  
  <p>To setup a comparator it is necessary to create a new class which
  implements this interface.</p>
  
  <p>Two basic comparators are provided by this library. A <code>StringComparator</code> in
  order to sort string literals and a <code>NumericComparator</code> to sort numbers.</p>
  
  <p id="link_ComparatorImplementationExample"><strong>Comparator implementation example</strong></p>
  
  <a class="exampleCollapsed" href="#link_ComparatorImplementationExample" onClick="toggleExample(this, 'ComparatorImplementationExample');">Show example</a><br /><div id="ComparatorImplementationExample" style='display:none'><div class='listing'><pre>package {
    import org.as3commons.collections.ArrayList;
    import flash.display.Sprite;

    public class ComparatorImplementationExample extends Sprite {

        public function ComparatorImplementationExample() {
            var list : ArrayList = new ArrayList();
            list.array = [3, 2, 5, 9, 4, 1, 3, 4, 6, 8];
            list.sort(new OddEvenComparator());
            trace (list.toArray()); // 1,3,3,5,9,2,4,4,6,8
        }
    }
}

import org.as3commons.collections.framework.IComparator;
import org.as3commons.collections.utils.UncomparableType;

internal class OddEvenComparator implements IComparator {

    public function compare(item1 :  item2 :  : int {
        if (isNaN(item1)) throw new UncomparableType(Number, item1);
        if (isNaN(item2)) throw new UncomparableType(Number, item2);
        
        // sort numeric if both items odd or even
        if ((item1 + item2) % 2 == 0) {
            if (item1 &lt; item2) return -1;
            if (item1 &gt; item2) return 1;
            return 0;
        }
        
        // sort odd numbers before even numbers
        if (item1 % 2) return -1; // item1 odd, item2 even
        return 1; // item1 even, item2 odd
    }
}
</pre></div></div>
  
  <p></p>
<p>
<span class="classHeaderTableLabel">See also</span>
</p>
<div class="seeAlso">
<a href="../utils/NumericComparator.html" target="">NumericComparator - Numeric comparator implementation.</a>
<br>
<a href="../utils/StringComparator.html" target="">StringComparator - Literal comparator implementation.</a>
</div>
<br>
<hr>
</div>
<a name="methodSummary"></a>
<div class="summarySection">
<div class="summaryTableTitle">Public Methods</div>
<table id="summaryTableMethod" class="summaryTable " cellpadding="3" cellspacing="0">
<tr>
<th>&nbsp;</th><th colspan="2">Method</th><th class="summaryTableOwnerCol">Defined&nbsp;by</th>
</tr>
<tr class="">
<td class="summaryTablePaddingCol">&nbsp;</td><td class="summaryTableInheritanceCol">&nbsp;</td><td class="summaryTableSignatureCol">
<div class="summarySignature">
<a class="signatureLink" href="#compare()">compare</a>(item1:*, item2:*):int</div>
<div class="summaryTableDescription">
   Compares two items.</div>
</td><td class="summaryTableOwnerCol">IComparator</td>
</tr>
</table>
</div>
<script type="text/javascript" language="javascript">
<!--
showHideInherited();
--></script>
<div class="MainContent">
<a name="methodDetail"></a>
<div class="detailSectionHeader">Method detail</div>
<a name="compare()"></a>
<table cellspacing="0" cellpadding="0" class="detailHeader">
<tr>
<td class="detailHeaderName">compare</td><td class="detailHeaderParens">()</td><td class="detailHeaderType">method</td>
</tr>
</table>
<div class="detailBody">
<code>public function compare(item1:*, item2:*):int</code><p>
   Compares two items.
   
   </p><p>There are two requirements to implementations of this method.</p>
   <ol>
   <li>The method is supposed to throw an exception, if an item cannot be
   compared due to a type mismatch. In such a case the sort operation
   cannot be performed, which is a critical error.</li>
   <li>Comparing a wrong typed item with oneself should throw an error.
   Adding a wrong typed item as the first element in a still empty sorted
   structure (such as <code>Treap</code> or <code>SortedList</code>) would
   be successful without that exception, since there is no comparision
   to other elements, that could fail. Thus, the sorted structures test
   the initial item against itself.</li>
   </ol>
   
   <span class="label">Parameters</span>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20px"></td><td><code><span class="label">item1</span>:*</code> &mdash; The first operant.
   </td>
</tr>
<tr>
<td class="paramSpacer">&nbsp;</td>
</tr>
<tr>
<td width="20px"></td><td><code><span class="label">item2</span>:*</code> &mdash; The second operant.
   </td>
</tr>
</table>
<p></p>
<span class="label">Returns</span>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20"></td><td><code>int</code></td>
</tr>
</table>
<br>
<span class="label">Throws</span>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="20"></td><td><code><a href="../utils/UncomparableType.html">UncomparableType</a> </code> &mdash; if eather of the items cannot be compared.
   </td>
</tr>
</table>
</div>
<br>
<br>
<hr>
<br>
<p></p>
<center class="copyright"> 
</center>
</div>
</body>
</html>
<!-- -->
